package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    //修改地址
    @PutMapping
    public R updateAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.updateAddressBook(addressBook);
    }

    //删除地址
    @DeleteMapping
    public R deleteAddressBook(@RequestParam("ids") List<Long> ids){
        return addressBookService.deleteAddressBook(ids);
    }

    //查询指定用户的全部地址
    @GetMapping("/list")
    public R getAllAddressBook(HttpSession session){
        return addressBookService.getAllAddressBook(session);
    }
    //查询默认地址
    @GetMapping("/default")
    public R getDefaultAddressBook(HttpSession session){
        return addressBookService.getDefaultAddressBook(session);
    }
    //根据id查询地址
    @GetMapping("/{id}")
    public R getAddressBookBYiId(@PathVariable Long id){
          return addressBookService.getAddressBookBYiId(id);
    }
    //设置默认地址
    @PutMapping("/default")
    public R setDefaultAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.setDefaultAddressBook(addressBook);
    }
    //新增地址
    @PostMapping
    public R saveAddressBook(@RequestBody AddressBook addressBook){
        return addressBookService.saveAddressBook(addressBook);
    }
}
